package tp.pr5;

import tp.pr5.instructions.DropInstruction;
import tp.pr5.instructions.HelpInstruction;
import tp.pr5.instructions.Instruction;
import tp.pr5.instructions.MoveInstruction;
import tp.pr5.instructions.OperateInstruction;
import tp.pr5.instructions.PickInstruction;
import tp.pr5.instructions.QuitInstruction;
import tp.pr5.instructions.RadarInstruction;
import tp.pr5.instructions.ScanInstruction;
import tp.pr5.instructions.TurnInstruction;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;

/**
 * The interpreter is in charge of converting the user input in an instruction for the robot. Up to now, the valid instructions are:
 *	MOVE
 *	TURN { LEFT | RIGHT }
 *  PICK <ITEM>
 *  SCAN [<ITEM>]
 *  OPERATE <ITEM>
 *	HELP 
 *	QUIT
 * @author VICTOR & HENAR
 *
 */
public class Interpreter {

	
	private static Instruction[] prototypes = new Instruction[]
	{
		new HelpInstruction(),
		new DropInstruction(),
		new MoveInstruction(),
		new OperateInstruction(),
		new PickInstruction(),
		new QuitInstruction(),
		new RadarInstruction(),
		new ScanInstruction(),
		new TurnInstruction()
	};
	/**
	 * Generates a new instruction according to the user input
	 * @param line - A string with the user input
	 * @return The instruction read from the given line. If the instruction is not correct, then it returns a not valid instruction, i.e., an empty instruction (invoking to the default constructor of class Instruction
	 * @throws WrongInstructionFormatException 
	 */
	public static Instruction generateInstruction(String line) throws WrongInstructionFormatException
	{
		for (int i = 0; i < prototypes.length; i++) 
		{
			try
			{
				return prototypes[i].parse(line);
			} 
			catch (Exception e)
			{			
			}
		}
		throw new WrongInstructionFormatException(Constants.MESSAGE_SAYS + Constants.MESSAGE_WHAT);
	}

	
	/**
	 * It returns information about all the instructions that the robot understands
	 * @return A string with the information about all the available instructions
	 */
	public static String interpreterHelp()
	{
		String help = "";
		
		for (int i = 0; i < prototypes.length; i++) 
		{
			help += prototypes[i].getHelp() + Constants.LINE_SEPARATOR;
		}
		return Constants.MESSAGE_HELP + Constants.LINE_SEPARATOR + help;
	}
	
}
